package com.boot;

import org.springframework.util.StopWatch;

import java.io.*;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Administrator
 * @className FileCompareTest
 * @description TODO
 * @date 2024/1/26 15:18
 **/
public class FileCompareTest {
    private static final int CHUNK_SIZE = 1024 * 1024; // 1MB chunk size

    public static void main(String[] args) {
        String filePath = "D:\\file2.text";
//        long fileSizeInBytes = 500 * 1024 * 1024; // 500MB
//        int stringLength = 30;
        StopWatch sw = new StopWatch();
//        sw.start("文件写入500M耗时： ");
//        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
//            long currentSize = 0;
//            SecureRandom secureRandom = new SecureRandom();
//
//            while (currentSize < fileSizeInBytes) {
//                String randomString = generateRandomString(stringLength, secureRandom);
//                writer.write(randomString);
//                writer.newLine();
//                currentSize += randomString.length() + System.lineSeparator().length();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        sw.stop();
//        System.out.println(sw.prettyPrint());



        String filePath1 = "D:\\file1.text";
        String filePath2 = "D:\\file2.text";
        sw.start("文件比较耗时： ");
        Set<String> commonStrings = findCommonStrings(filePath1, filePath2);
        sw.prettyPrint();
        System.out.println("共有 " + commonStrings.size() + " 个相同的字符串:");
        for (String commonString : commonStrings) {
            System.out.println(commonString);
        }
        System.out.println(sw.prettyPrint());
    }

    private static String generateRandomString(int length, SecureRandom secureRandom) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder randomString = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int randomIndex = secureRandom.nextInt(characters.length());
            randomString.append(characters.charAt(randomIndex));
        }

        return randomString.toString();
    }

    private static Set<String> findCommonStrings(String filePath1, String filePath2) {
        Set<String> commonStrings = new HashSet<>();

        try (BufferedReader reader1 = new BufferedReader(new FileReader(filePath1));
             BufferedReader reader2 = new BufferedReader(new FileReader(filePath2))) {

            String chunk1;
            String chunk2;

            while ((chunk1 = readChunk(reader1)) != null && (chunk2 = readChunk(reader2)) != null) {
                Set<String> set1 = new HashSet<>();
                Set<String> set2 = new HashSet<>();

                String[] lines1 = chunk1.split(System.lineSeparator());
                String[] lines2 = chunk2.split(System.lineSeparator());

                for (String line : lines1) {
                    set1.add(line);
                }

                for (String line : lines2) {
                    set2.add(line);
                }

                set1.retainAll(set2);
                commonStrings.addAll(set1);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return commonStrings;
    }

    private static String readChunk(BufferedReader reader) throws IOException {
        StringBuilder chunk = new StringBuilder();
        int bytesRead = 0;
        char[] buffer = new char[CHUNK_SIZE];

        while (bytesRead < CHUNK_SIZE && reader.read(buffer) != -1) {
            chunk.append(buffer);
            bytesRead += buffer.length;
        }

        return chunk.toString();
    }


}
